﻿package com.gamemeal.particle.steering {
	import com.gamemeal.geom.VelocityPoint3;
	/**
	 * @author Colin
	 */
	public class Seek implements ISteering{
		private var imp:ISteering;
		public function Seek(enable3D:Boolean=true){
			if(enable3D){
				imp = new impSeek3D();
			}else{
				imp = new impSeek2D();
			}
		}
		public function apply(p:VelocityPoint3,tx:Number,ty:Number,tz:Number=null):void{
			imp.apply(p,tx,ty,tz);
		}
	}
}


import com.gamemeal.geom.VelocityPoint3;

class impSeek2D implements ISteering{
	public function apply(p:VelocityPoint3,tx:Number,ty:Number,tz:Number=null):void{
		tz;
		var dx:Number;
		var dy:Number;
		var dl:Number;
		
		//seek vector
		dx = tx - p.x;
		dy = ty - p.y;

		dl = Math.sqrt(dx*dx+dy*dy);

		var vl:Number = Math.sqrt(p.vx*p.vx+p.vy*p.vy);
		p.vx+=dx;
		p.vy+=dy;

		//set new speed as old speed;
		var scale:Number = vl/Math.sqrt(p.vx*p.vx+p.vy*p.vy);
		p.vx*=scale;
		p.vy*=scale;
	}
}

class impSeek3D implements ISteering{
	public function apply(p:VelocityPoint3,tx:Number,ty:Number,tz:Number=null):void{
		var dx:Number;
		var dy:Number;
		var dz:Number;
		var dl:Number;
		
		//seek vector
		dx = tx - p.x;
		dy = ty - p.y;
		dz = tz - p.z;

		dl = Math.sqrt(dx*dx+dy*dy+dz*dz);

		var vl:Number = Math.sqrt(p.vx*p.vx+p.vy*p.vy+p.vz*p.vz);
		p.vx+=dx;
		p.vy+=dy;
		p.vz+=dz;

		//set new speed as old speed;
		var scale:Number = vl/Math.sqrt(p.vx*p.vx+p.vy*p.vy+p.vz*p.vz);
		p.vx*=scale;
		p.vy*=scale;
		p.vz*=scale;
	}
}
